package com.wedata.抖音;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.wedata.HttpUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

public class 抖音来客会员 {

    private static String uri = "http://localhost:8090";
    //private static String uri = "https://twedata.gymcity.com.cn";
    //private static String uri = "https://wedata.gymcity.com.cn";

    public static void main(String[] args) {

        //模拟入会通知();
        //模拟退会通知();
        模拟修改通知();
        //通知抖音修改会员信息();

        String string = "5706975dc56ff751fe60b16a797e8c01&client_key=awu1dbdfx5yj4r5e&timestamp=1753692239891&http_body={\"update_time\":\"1753692239\",\"info\":{\"mobile\":{\"old_mobile\":\"K5sw75CjugikJqlQo7Q3sQ==\",\"new_mobile\":\"uTSM8UCipPtrFzdZrL+ojA==\"}},\"open_id\":\"_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n\",\"account_id\":\"7011378472920713250\"}";
        //，xLifeSign:b6a97cf074a9e2d774390114446e0e59565171099de30cffc59f5d4d5694845f
        System.out.println(DigestUtils.sha256Hex(string));
    }

    private static void 模拟入会通知(){
        String url = uri + "/datacenter/api/callback/dyLaiKeMemberJoin?client_key=awu1dbdfx5yj4r5e&sign=ff2fbce12b9e06d183a7875aa5c7cb17&timestamp=1753425882939";
        JSONObject header = new JSONObject();
        JSONObject body = new JSONObject();
        body.set("open_id","_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n");
        body.set("account_id","7011378472920713250");
        body.set("mobile","13290053690");
        Map<String, Object> request = new HashMap<>();
        request.put("client_key", "awu1dbdfx5yj4r5e");
        request.put("sign", "ff2fbce12b9e06d183a7875aa5c7cb17");
        request.put("timestamp", 1753425882939L);
        header.set("x-life-sign", verifyDyLaiKeMsg(request,body));
        HttpUtils.sendPost(url, header, body);
    }
    private static void 模拟退会通知(){
        String url = uri + "/datacenter/api/callback/dyLaiKeMemberLeave?client_key=awu1dbdfx5yj4r5e&sign=ff2fbce12b9e06d183a7875aa5c7cb17&timestamp=1753425882939";
        JSONObject header = new JSONObject();
        JSONObject body = new JSONObject();
        body.set("open_id","_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n");
        body.set("account_id","7011378472920713250");
        body.set("mobile","0");
        Map<String, Object> request = new HashMap<>();
        request.put("sign", "ff2fbce12b9e06d183a7875aa5c7cb17");
        request.put("client_key", "awu1dbdfx5yj4r5e");
        request.put("timestamp", 1753425882939L);
        header.set("x-life-sign", verifyDyLaiKeMsg(request,body));
        HttpUtils.sendPost(url, header, body);
    }
    private static void 模拟修改通知(){
        String url = uri + "/datacenter/api/callback/dyLaiKeMemberUpdate?client_key=awu1dbdfx5yj4r5e&sign=ff2fbce12b9e06d183a7875aa5c7cb17&timestamp=1753692239891";
        JSONObject header = new JSONObject();
        JSONObject body = new JSONObject("{\"update_time\":\"1753692239\",\"info\":{\"mobile\":{\"old_mobile\":\"K5sw75CjugikJqlQo7Q3sQ==\",\"new_mobile\":\"uTSM8UCipPtrFzdZrL+ojA==\"}},\"open_id\":\"_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n\",\"account_id\":\"7011378472920713250\"}");
        //JSONObject body = new JSONObject("{\"update_time\":\"1753692239\",\"info\":{\"mobile\":{\"old_mobile\":\"uTSM8UCipPtrFzdZrL+ojA==\",\"new_mobile\":\"K5sw75CjugikJqlQo7Q3sQ==\"}},\"open_id\":\"_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n\",\"account_id\":\"7011378472920713250\"}");
        /*body.set("open_id","_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n");
        body.set("account_id","7011378472920713250");
        body.set("update_time","2025-07-22");
        JSONObject info = new JSONObject();
        JSONObject mobile = new JSONObject();
        mobile.set("old_mobile","BnhtaTW0Z1CROeMY9SG15Q==");
        mobile.set("new_mobile","BnhtaTW0Z1CROeMY9SG15Q==");
        info.set("mobile",mobile);
        body.set("info",info);*/

        Map<String, Object> request = new HashMap<>();
        request.put("client_key", "awu1dbdfx5yj4r5e");
        request.put("sign", "ff2fbce12b9e06d183a7875aa5c7cb17");
        request.put("timestamp", 1753692239891L);
        header.set("x-life-sign", verifyDyLaiKeMsg(request,body));
        HttpUtils.sendPost(url, header, body);
    }
    public static void 通知抖音修改会员信息() {
        String url = "https://open.douyin.com/goodlife/v1/member/user/update/";

        JSONObject header = new JSONObject();
        header.put("access-token",抖音来客.postRequestWithSign());
        JSONObject body = new JSONObject();
        body.set("account_id", "7011378472920713250");
        List<JSONObject> list = new ArrayList<>();
        JSONObject member = new JSONObject();
        member.set("open_id", "_000NaedYC5bA_TDik4zJbFb1Hq8yh3ZBX_n");
        member.set("points_update_cent", 2000);
        member.set("points_amount_cent", 2000);
        member.set("user_level", 2);
        member.set("unbind", true);
        //member.set("new_mobile", "18867363985");
        member.set("update_time", System.currentTimeMillis()/1000);
        list.add(member);
        body.set("members",list);
        JSONObject result = HttpUtils.sendPost(url ,header,body);
        System.out.println(body);
        System.out.println(result);
    }
    private static String verifyDyLaiKeMsg(Map<String, Object> request, JSONObject bodyMap) {
        String str1 = "5706975dc56ff751fe60b16a797e8c01";
        List<String> paramList = new ArrayList<>();
        for (String paramName : request.keySet()){
            if("sign".equals(paramName)){
                continue;
            }
            paramList.add("&"+paramName+"="+request.get(paramName));
        }
        //给paramList排序
        paramList.sort(Comparator.naturalOrder());
        for (String param : paramList) {
            str1 += param;
        }
        str1 = str1+"&http_body="+ JSON.toJSONString(bodyMap);
        System.out.println(str1);
        return DigestUtils.sha256Hex(str1);
    }

}
